Skip to content

Update InsertionSort.java#64

Open
Vsareen0 wants to merge 2 commits intoronijpandey:masterfrom
Vsareen0:patch-1
Open

Update InsertionSort.java#64
Vsareen0 wants to merge 2 commits intoronijpandey:masterfrom
Vsareen0:patch-1

Conversation

@Vsareen0
Copy link

Comments have been added for the implementation of insertion sort algorithm

@Shayan-Asgari
Copy link

In the insertion sort when you put "arr[j+1] = key;", I believe it is unnecessary since you have arr[j+1] = arr[j] where arr[i] will always be key.

Regards, Shayan Asgari

@Shayan-Asgari
Copy link

Inside the while loop there is also a problem. You do not do any swapping of integers, you are simply overriding the original array! You must have:
while (j>=0 && arr[j] > key)
{
int temp = arr[j]; // save arr[j] into a temporary
arr[j] = arr[j+1] ; // [J+1] will always be key
arr[j+1] = temp;
j = j--;
}
//arr[j+1] = key; <-------There is no need for this line

Copy link
Author

@Vsareen0 Vsareen0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a look at modifications !

@Shayan-Asgari
Copy link

I believe the code is still incorrect. You are overriding some of the position values.

Regards,
Shayan Asgari

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments